//_ = require('underscore');

function JabaResult(resultset) {
	
	this.iterations = {};
	this.data = {
		strings: {},
		numbers: {},
		dates: {}
	};

	for( var name in resultset ) {
		if ( resultset[name] instanceof Array ) {
			this.iterations[name] = [];
			var self = this;
			resultset[name].forEach(function(result) {
				self.iterations[name].push(new JabaResult(result));
			});
		}
		else if( typeof(resultset[name]) == "string" ) {
			this.data.strings[name] = resultset[name];
		}
		else if( typeof(resultset[name]) == "number" ) {
			this.data.numbers[name] = resultset[name];
		}
		else if (typeof(resultset[name]) == "object" 
			&& typeof(resultset[name]['*Datetime']) != 'undefined') {

			var date = resultset[name]['*Datetime'];
			var parts = date.split(' ');
			
			date = parts[0];
			var time = parts[1];

			parts = date.split('-');
			parts = parts.concat(time.split(':'));

			if( parts.length != 6 ) {
				this.data.strings[name] = 'invalid date';
				continue;
			};

			for( var i = 0; i < 6; i++ ) {
				if( i == 1 ) parts[i] = parseInt(parts[i], 10) - 1;
				else parts[i] = parseInt(parts[i], 10);
			}

			this.data.strings[name] = new Date(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]).toString();
		} 
	}
}

JabaResult.prototype.getIteration = function(name) {
	if( typeof(this.iterations[name]) == 'undefined' ) {
		return [];
	}
	return this.iterations[name];
}

JabaResult.prototype.getString = function(name) {
	if( typeof(this.data.strings[name]) != 'undefined') { 
		return this.data.strings[name];
	}
	else if( typeof(this.data.numbers[name]) != 'undefined') {
		return this.data.numbers[name];
	}
	throw "String " + name + " is not defined";
}

/*
JabaResult.prototype.getNumber = function(name) {
	if( typeof(this.data.numbers[name]) == 'undefined') {
		throw "Number " + name + " is not defined";
	}
	return this.data.numbers[name];
}

JabaResult.prototype.getDate = function(name) {
	if( typeof(this.data.dates[name]) == 'undefined') {
		throw "Date " + name + " is not defined";
	}
	return this.data.dates[name];
}
*/

exports.JabaResult = JabaResult;